
<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'PHPUnit/Framework.php';
require_once 'autoload.php';

class TestArticle extends PHPUnit_Framework_TestCase {
	
	function test_check_contents_with_valid() {
		
		$values = array('body' => 'uh yeah');
		
		$checker = $this->getMock('InputChecker');
		$checker->expects($this->once())
				->method("check_tags_advanced")
				->with($values['body'], "body")
				->will($this->returnValue(true));
		$checker->expects($this->once())
				->method("check_tag_closures")
				->with($values['body'])
				->will($this->returnValue(true));
		$checker->expects($this->once())
				->method("check_exists")
				->with($values['body'], "body")
				->will($this->returnValue(true));

		$page = new Page();
		$article = new Article($page);
		$article->set_input_checker($checker);

		$this->assertTrue($article->check_content($values));

	}

	function test_check_contents_with_first_fails() {
		
		$values = array('body' => 'uh yeah');
		
		$checker = $this->getMock('InputChecker');
		$checker->expects($this->once())
				->method("check_tags_advanced")
				->with($values['body'], "body")
				->will($this->returnValue(false));
		$checker->expects($this->never())
				->method("check_tag_closures");
		$checker->expects($this->never())
				->method("check_exists");

		$page = new Page();
		$article = new Article($page);
		$article->set_input_checker($checker);

		$this->assertFalse($article->check_content($values));

	}

	function test_check_contents_with_second_fails() {
		
		$values = array('body' => 'uh yeah');
		
		$checker = $this->getMock('InputChecker');
		$checker->expects($this->once())
				->method("check_tags_advanced")
				->with($values['body'], "body")
				->will($this->returnValue(true));
		$checker->expects($this->once())
				->method("check_tag_closures")
				->with($values['body'])
				->will($this->returnValue(false));
		$checker->expects($this->never())
				->method("check_exists");

		$page = new Page();
		$article = new Article($page);
		$article->set_input_checker($checker);

		$this->assertFalse($article->check_content($values));
	}

	function test_get_edit_body() {

		$article_id = 566;
		$page = new Page();
		$article = new Article($page, $article_id);

		$expected = array('body' => 'Keep your paws to yourself.');
		
		$db_access = $this->getMock('DbAccessor');
		$db_access->expects($this->once())
				  ->method('db_query_select')
				  ->with(array(array('*')), array("ARTICLE"), "item_id = $article_id")
				  ->will($this->returnValue('SelectStatement'));

		$db_access->expects($this->once())
				  ->method('db_do_query')
				  ->with('SelectStatement')
				  ->will($this->returnValue('QueryResult'));

		$db_access->expects($this->once())
				  ->method('db_fetch_array')
				  ->with('QueryResult')
				  ->will($this->returnValue($expected));

		$article->set_db_accessor($db_access);
		$this->assertEquals($expected['body'], $article->get_edit_body());
	}

	function test_prepare() {

		$item  = array('body' => "Let's prod some buttock!");

		$page = new Page();
		$article = new Article($page);

		$creator = $this->getMock('FormCreator');
		$creator->expects($this->once())
				->method('input_text')
				->with('Artikel', 'body', 'desqlized_string')
				->will($this->returnValue('input_creator_result'));

		$db_access = $this->getMock('DbAccessor');
		$db_access->expects($this->once())
				  ->method('db_desqlize')
				  ->with($item['body'])
				  ->will($this->returnValue('desqlized_string'));

		$article->set_form_creator($creator);
		$article->set_db_accessor($db_access);

		$actual = $article->prepare(false, $item, array());
		$expected = array('input_creator_result');

		$this->assertEquals($expected, $actual);
	}

	function test_derive_info() {
		$expected_post_link = "post_link";
		$_POST['link'] = $expected_post_link;

		$page = new Page();
		$article = new ArticleTest($page);
		$actual = $article->derive_info_test();
		$this->assertEquals($expected_post_link, $actual);
	}

	function test_derive_info_with_long_value() {
		$expected_post_link = "post_link_of_way_over_fifty_characters_you_know_" .
				"the_type_where_the_text_just_keeps_going_on_and_on_indefinitely_" .
				"till_someone_finally_decides_that_pinocchio_had_nothing_to_do" .
				"with_it_in_the_first_place";
		$_POST['link'] = $expected_post_link;

		$page = new Page();
		$article = new ArticleTest($page);
		$actual = $article->derive_info_test();
		$expected = substr($expected_post_link, 0, 50) . "&hellip;"; 
		$this->assertEquals($expected, $actual);
	}

	function test_get_sql_from_post() {

		$expected_post_body = 'post_body';
		$expected_sqlized_post_body = 'sqlized_post_body';
		$expected_input_query = 'input_query';
		$expected_item_id = 7;

		$_POST['body'] = $expected_post_body;

		$page = new Page();
		$article = new ArticleTest($page);

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_sqlize')
				->with($expected_post_body)
				->will($this->returnValue($expected_sqlized_post_body));
		$mock_db_access->expects($this->once())
				->method('db_query_insert')
				->with(array('item_id', 'body'), 
						array($expected_item_id, $expected_sqlized_post_body), 
						'ARTICLE')
				->will($this->returnValue($expected_input_query));
		$article->set_db_accessor($mock_db_access);

		$actual = $article->get_sql_from_post_test($expected_item_id);
		$this->assertEquals($expected_input_query, $actual);
	}
}

class ArticleTest extends Article
{
	function derive_info_test() {
		return $this->derive_info();
	}

	function get_sql_from_post_test($item_id) {
		return $this->get_sql_from_post($item_id);
	}
}
